import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hxdj/common/colors/my_colors.dart';
import 'package:hxdj/common/components/my_image.dart';
import 'package:hxdj/message/message_page/message_viewmodel.dart';
import 'package:provider/provider.dart';

class TabModel {
  ///选中的图片
  String activeImgPath;

  ///未选中图片
  String normalImgPath;

  ///tab名字
  String label;

  TabModel(this.activeImgPath, this.normalImgPath, this.label);
}

class CABottomNavigationBar extends StatefulWidget {
  const CABottomNavigationBar({
    super.key,
    required this.length,
    required this.currentIndex,
    required this.callback,
    required this.tabList,
  });
  final Function(int) callback;
  final int length;
  final int currentIndex;
  final List<TabModel> tabList;

  @override
  State<StatefulWidget> createState() {
    return _CABottomNavigationBar();
  }
}

class _CABottomNavigationBar extends State<CABottomNavigationBar> {
  //点击回调
  void _onTapHandler(int index) {
    if (widget.currentIndex == index) return;
    widget.callback(index);
  }

  @override
  Widget build(BuildContext context) {
    double bottomPadding = MediaQuery.of(context).padding.bottom;

    return Container(
      decoration: const BoxDecoration(boxShadow: <BoxShadow>[
        BoxShadow(
          color: Color.fromRGBO(0, 0, 0, 0.05),
          offset: Offset(0.0, -13),
          blurRadius: 12.0,
          spreadRadius: -9.0,
        )
      ]),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
              height: kToolbarHeight,
              color: Colors.white,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: widget.tabList.map((e) {
                    return getTabWidget(e);
                  }).toList())),
          Container(
            height: bottomPadding,
            color: Colors.white,
          ),
        ],
      ),
    );
  }

  Widget getTabWidget(TabModel tabModel) {
    var indexOf = widget.tabList.indexOf(tabModel);
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        _onTapHandler(indexOf);
      },
      child: _buildButtomBarItem(
        isActive: widget.currentIndex == indexOf,
        activeImgPath: tabModel.activeImgPath,
        normalImgPath: tabModel.normalImgPath,
        label: tabModel.label,
      ),
    );
  }

  Widget _buildButtomBarItem({
    required bool isActive,
    required String activeImgPath,
    required String normalImgPath,
    required String label,
  }) {


    Widget _buildItem() {
      //头像
      return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        MyImage(imageName: isActive ? activeImgPath : normalImgPath, width: 20.w, height: 20.w),
        SizedBox(height: 3.w),
        Text(
          label,
          style: TextStyle(color: isActive ? MyColours.appMain : MyColours.color9B9B9B, fontSize: 12.sp),
        ),
      ]);
    }

    return label == "消息"
        ? Consumer<MessageViewModel>(builder: (context, model, child) {
            debugPrint("这里会刷新吗? ");
            int count = model.allUnreadMessageCount;
            //刷新完成
            return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
              Stack(
                clipBehavior: Clip.none,
                children: [
                  MyImage(imageName: isActive ? activeImgPath : normalImgPath, width: 20.w, height: 20.w),
                  Positioned(
                    right: -5.w,
                    top: -5.w,
                    child: (label == "消息" && count > 0)
                        ? Container(
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: MyColours.colorE33432,
                              shape: BoxShape.rectangle,
                              borderRadius: BorderRadius.all(Radius.circular(8.w)),
                            ),
                            padding: EdgeInsets.only(left: 2.w, right: 3.w),
                            constraints: BoxConstraints(
                              minWidth: 15.w,
                              maxHeight: 15.w,
                              minHeight: 13.w,
                            ),
                            child: Text(count.toString(),
                                textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontSize: 11.sp)),
                          )
                        : Container(),
                  ),
                ],
              ),

              SizedBox(height: 3.w),
              Text(
                label,
                style: TextStyle(color: isActive ? MyColours.appMain : MyColours.color9B9B9B, fontSize: 12.sp),
              ),
            ]);
          })
        : _buildItem();



   
  }
}
